<template>
    <div>
        <div class="info-form">
            <el-form :model="addForm" :inline="true">
                <el-form-item>
                    <el-select v-model="value"
                               filterable
                               allow-create
                               placeholder="请选择或输入考试描述"
                               class="goodstype-input">
                        <el-option
                                style="width: 100px;"
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm()" v-if="addForm.switch">提交</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div>
            <el-table
                    :data="tableData"
                    stripe
                    style="width: 100%">
                <el-table-column
                        prop="lname"
                        label="考试科目">
                </el-table-column>
                <el-table-column
                        prop="examTime"
                        label="考试时间">
                </el-table-column>
                <el-table-column
                        prop="startTime"
                        label="开始时间">
                </el-table-column>
                <el-table-column
                        prop="endTime"
                        label="结束时间">
                </el-table-column>
                <el-table-column label="操作">
                    <template scope="scope">
                        <el-button @click="handleClick(scope.$index)" type="text" size="small">编辑</el-button>
                        <!--<el-button @click.native.prevent="deleteRow(scope.$index, tableData)" type="text" size="small"> 删除-->

                        <!--</el-button>-->
                    </template>
                </el-table-column>
            </el-table>
            <el-dialog title="编辑时间" :visible.sync="dialogTableVisible">
                <el-date-picker
                        v-model="value1"
                        type="date"
                        placeholder="选择日期"
                        :picker-options="pickerOptions0">
                </el-date-picker>
                <el-time-picker
                        is-range
                        v-model="value2"
                        placeholder="选择时间范围">
                </el-time-picker>
                <div style="margin-top: 20px">
                    <el-button type="primary" @click="setTime()">提交</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    import moment from 'moment';
    import ElFormItem from "../../../../node_modules/element-ui/packages/form/src/form-item";
    export default {
        components: {ElFormItem},
        data() {
            return {
                index: '',
                options: [],
                value: '',
                value1: '',
                value2: '',
                addForm: {},
                tableData: [],
                lessons: [],
                stuData: [],
                dialogTableVisible: false,
                pickerOptions0: {
                    disabledDate(time) {
                        return time.getTime() < Date.now() - 8.64e7;
                    }
                },

            }
        },
        activated(){
            let user = JSON.parse(localStorage.getItem("currentUser"));
            if (user.account == "system") {
                this.addForm.switch = true;
            }
            this.getExamDescription();
            this.getLessons();
//            this.getExam();

        },
        methods: {
            submitForm(){
                let self = this;
                let des = this.value;
                if (!des.id) {
                    this.$axioxs.post("/exam_description/save?des=" + des)
                        .then(function (res) {
                            self.setExam(res.data);
                        })
                } else {
                    this.setExam(des);
                }

            },
            setExam(val){
                let exams = [];
                let self = this;
                this.tableData.forEach(item => {
                    let exam = {
                        lid: item.id,
                        examTime: item.exam_time,
                        startTime: item.start_time,
                        endTime: item.end_time,
                        desid: val.id
                    }
                    if (item.examTime != '') {
                        console.log(exam)
                        this.$axioxs.post("/exam/save", exam)
                            .then(function (res) {
                                self.$router.push({
                                    path: '/exam',
//                                    query: {item: this.tableData[index]}
                                });
                            })
                    }
                })
            },
            getLessons(){
                let self = this;
                this.tableData = [];
                this.$axioxs.get("/lessons/get")
                    .then(function (res) {
                        res.data.records.forEach(item => {
                            item.examTime = '';
                            item.startTime = '';
                            item.endTime = '';
                            self.tableData.push(item);
                        })
                    })
            },
            getExamDescription(){
                let self = this;
                this.options = []
                this.$axioxs.get("/exam_description/get")
                    .then(function (res) {
                        let i = res.data.records;
                        i.forEach(item => {
                            let j = {}
                            j.value = item;
                            j.label = item.examDescription;
                            self.options.push(j);
                        })

                    })
            },
            handleClick(index) {
                this.dialogTableVisible = true;
                this.index = index;
            },
            setTime(){
                this.tableData[this.index].examTime = moment(this.value1).format("YYYY-MM-DD");
                this.tableData[this.index].startTime = moment(this.value2[0]).format("hh:mm:ss");
                this.tableData[this.index].endTime = moment(this.value2[1]).format("hh:mm:ss");
                this.tableData[this.index].exam_time = this.value1;
                this.tableData[this.index].start_time = this.value2[0];
                this.tableData[this.index].end_time = this.value2[1];
                this.dialogTableVisible = false;
            },
            deleteRow(index, rows) {
                rows.splice(index, 1);
            }
        }
    }
</script>